<?php

namespace App\Controller\Admin;

use App\Base\FormCreator;
use App\Model\AdminMenuModel;
use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController4Admin;
use App\Entity\AdminMenu as Entity;
use App\Setting\AdminMenuSetting;
use App\Repository\AdminMenuRepository;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/admin/")
 */
class AdminMenuController extends BaseController4Admin
{

    /**
     * @Route("AdminMenu/add",name="AdminMenuPreAdd")
     */
    public function add(AdminMenuSetting $setting,FormCreator $formCreator,AdminMenuRepository $repository)
    {
        $uid = $this->needToBeAdmin();
        $entity = new Entity();
        if ( $parent = $this->getParamRequired("parent")) {
            $entity->setParent($repository->find($parent));
        }
        $from = $formCreator->createAddFrom($entity,$setting->formConfig(),"");
        $data = array(
            "title" => $setting->entityCnName,
            "show"   => $from->createView(),
            "isAdd"   => true,
        );
        return $this->show("Admin/AdminMenu/edit.php.twig",$data);
    }

    /**
     * @Route("AdminMenu/edit/{id}",name="AdminMenuPreEdit")
     */
    public function edit(Entity $entity)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new AdminMenuSetting($this->getDoctrine()));
        $setting = new AdminMenuSetting($this->getDoctrine());
        $from = $this->getFormCreator()->createAddFrom($entity,$setting->formConfig());
        $data = array(
            "title" => $this->entitySetting->entityCnName,
            "show"   => $from->createView(),
            "isAdd"   => false,
        );
        return $this->show("Admin/AdminMenu/edit.php.twig",$data);
    }

    /**
     * @Route("AdminMenu",name="AdminMenuLists")
     */
    public function lists(AdminMenuRepository $repository ,AdminMenuSetting $setting)
    {
        $uid = $this->needToBeAdmin();
        $mo = new AdminMenuModel($this->getDoctrine());
        $data = $mo->getAllAdminMenuTree();
        /*URL配置*/
        $urls = array(
            "toolUrl"=>$this->generateUrl($setting->entityName."Tools"),
            "addUrl"=>$this->generateUrl($setting->entityName."PreAdd"),
        );
        $entityCnName = $setting->entityCnName;
        $config = $setting->formConfig();
        $data = array(
            'title'     => "$entityCnName 列表",
//            "classes"   => $classes,
            'urls'      => $urls,
            'lists'     => $data,
            "config"    => $config,
        );
        return $this->show("Admin/AdminMenu/list.php.twig",$data);
    }


    /**
     * @Route("AdminMenu/tools",name="AdminMenuTools")
     */
    public function tools(Request $request)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new AdminMenuSetting($this->getDoctrine()));
        return $this->comTools($request);
    }


    /**
     * @Route("AdminMenu/copy/{id}",name="AdminMenuDoCopy",methods={"GET", "POST"})
     *
     */
    public function doCopy(Request $request,Entity $entity)
    {
        $tmp = $entity;
        $tmp = $this->toArray($tmp);
        $en = new Entity();
        foreach ($tmp as $key => $value) {
            if ($key == "id"||$key == "sons"||$key == "groups") {continue;}
            $funcGet = "get".ucfirst($key);
            $funcSet = "set".ucfirst($key);
            $en->$funcSet($entity->$funcGet());
        }
        $this->myEm()->persist($en);$this->myEm()->flush();
        return $this->redirectToRoute("AdminMenuLists");
    }

}
